package com.dxq.test.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dxq.test.App;
import com.dxq.test.common.AppManager;
import com.dxq.test.R;
import com.dxq.test.adapter.GroupAdapter;
import com.dxq.test.db.Group;
import com.dxq.test.db.Label;
import com.dxq.test.db.Pasture;
import com.dxq.test.db.TablesDao;
import com.j256.ormlite.dao.ForeignCollection;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: zsh
 * Date: 2023/11/13
 * Describe: 群列表页面
 */
public class GroupActivity extends AppCompatActivity implements View.OnClickListener {

    public TablesDao tablesDao;

    private ImageView ivBack, ivMore;
    private TextView tvTitle;
    private RecyclerView recyclerView;

    private GroupAdapter mAdapter;
    private List<Group> recordList = new ArrayList<>();

    private String sex = "";
    private String breedingStatus = "";
    private String QRCodeID = "";
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_group);
        tablesDao = new TablesDao(this);
        initView();
        initAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        ivMore = findViewById(R.id.ivMore);
        tvTitle = findViewById(R.id.tvTitle);
        tvTitle.setText("群列表");
        ivMore.setImageResource(R.drawable.ic_baseline_add_24);
        ivMore.setVisibility(View.VISIBLE);

        recyclerView = this.findViewById(R.id.recyclerView);

        ivBack.setOnClickListener(this);
        ivMore.setOnClickListener(this);
    }

    private void initAdapter() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//添加分割线
        mAdapter = new GroupAdapter(R.layout.item_group, recordList);//
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.layout_empty);

        //item点击事件
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter,
                                    @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("item事件");
                //跳转页面
                Intent intent = new Intent(GroupActivity.this, GroupDetailActivity.class);
                Group group = recordList.get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("group", group);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        //item子view点击事件
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter,
                                         @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("child添加事件");

            }
        });
    }

    private void addGroupDialog() {
        View view = getLayoutInflater().inflate(R.layout.add_group_dialog, null);
        AutoCompleteTextView eventName = view.findViewById(R.id.name);
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("添加群")//设置对话框的标题
                .setView(view)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name = eventName.getText().toString();
                        if (name.equals("")) {
                            ToastUtils.showShort("填写群名称");
                            return;
                        }
                        //跳转页面
                        Intent intent = new Intent(GroupActivity.this, StockActivity.class);
                        intent.putExtra("groupName", name);
                        startActivity(intent);
                    }
                }).create();
        dialog.show();
    }

    private void getData() {
        recordList.clear();
        Pasture pasture = tablesDao.queryPastureById(App.select_pasture_id);
        if (null != pasture) {
            ForeignCollection<Group> groups = pasture.getGroups();
            for (Group group : groups) {
                List<Label> labels = tablesDao.queryLabelForGroup(group);
                group.setLabelSize(labels.size());
                recordList.add(group);
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                GroupActivity.this.finish();
                break;

            case R.id.ivMore:
                addGroupDialog();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }
}
